package com.edu.exam.entity.assessment.standard;

import com.edu.exam.enums.assessment.AssessmentSubjectTypeCategory;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;

import java.time.LocalDateTime;

/**
 * 考核科目类型实体类
 * 所属模块：F-07 基础数据管理模块
 * 对应数据库表：assessment_subject_types
 *
 * 管理考核科目的分类标准，仅包含三个基础分类：形成性考核、终结性考核、其它。
 * 作为分类规范供业务模块使用，不包含具体考核科目。
 *
 * @author System
 * @version 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "assessment_subject_types")
@Comment("考核科目类型表")
public class AssessmentSubjectType {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 考核科目类型代码，唯一标识符
     */
    @NotBlank(message = "考核科目类型代码不能为空")
    @Size(max = 20, message = "考核科目类型代码长度不能超过20个字符")
    @Column(name = "type_code", nullable = false, unique = true, length = 20)
    private String typeCode;

    /**
     * 考核科目类型名称
     */
    @NotBlank(message = "考核科目类型名称不能为空")
    @Size(max = 50, message = "考核科目类型名称长度不能超过50个字符")
    @Column(name = "type_name", nullable = false, length = 50)
    private String typeName;

    /**
     * 考核科目类型类别
     */
    @NotNull(message = "考核科目类型类别不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "type_category", nullable = false, length = 20)
    private AssessmentSubjectTypeCategory typeCategory;

    /**
     * 描述信息
     */
    @Size(max = 500, message = "描述信息长度不能超过500个字符")
    @Column(name = "description", length = 500)
    private String description;

    /**
     * 是否系统预定义
     */
    @Column(name = "is_system")
    @Builder.Default
    private Boolean isSystem = true;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 创建人
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 更新人
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updatedTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedTime = LocalDateTime.now();
    }

    /**
     * 验证类型代码
     */
    public boolean validateTypeCode() {
        return typeCode != null && !typeCode.trim().isEmpty();
    }

    /**
     * 验证类型类别
     */
    public boolean validateTypeCategory() {
        return typeCategory != null;
    }

    /**
     * 判断是否为形成性考核
     */
    public boolean isFormativeAssessment() {
        return AssessmentSubjectTypeCategory.FORMATIVE.equals(typeCategory);
    }

    /**
     * 判断是否为终结性考核
     */
    public boolean isSummativeAssessment() {
        return AssessmentSubjectTypeCategory.SUMMATIVE.equals(typeCategory);
    }

    /**
     * 获取基础定义
     */
    public String getBasicDefinition() {
        if (description == null || description.trim().isEmpty()) {
            return typeName;
        }
        return typeName + "：" + description;
    }
}